﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using UPMS.Core.Domain;

namespace UPMS.Web.Controllers
{
    public class PermissionController : BaseController
    {
        // GET: /Permission/
        public ActionResult Index()
        {
            return View(permissonDao.Select());
        }

        // GET: /Permission/Details/5
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Permission permission = permissonDao.Select(id.Value);
            if (permission == null)
            {
                return HttpNotFound();
            }
            return View(permission);
        }

        // GET: /Permission/Create
        public ActionResult Create()
        {
            ViewBag.ModuleId = new SelectList(moduleDao.Select(), "Id", "Name");
            return View();
        }

        // POST: /Permission/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Status,ModuleId,Description,Created,CreatedBy,Modified,ModifiedBy")] Permission permission)
        {
            if (ModelState.IsValid)
            {
                permission.Id = Guid.NewGuid();
                permissonDao.Insert(permission);
                return RedirectToAction("Index");
            }

            ViewBag.ModuleId = new SelectList(moduleDao.Select(), "Id", "Name", permission.ModuleId);
            return View(permission);
        }

        // GET: /Permission/Edit/5
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Permission permission = permissonDao.Select(id.Value);
            if (permission == null)
            {
                return HttpNotFound();
            }
            ViewBag.ModuleId = new SelectList(moduleDao.Select(), "Id", "Name", permission.ModuleId);
            return View(permission);
        }

        // POST: /Permission/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,Status,ModuleId,Description,Created,CreatedBy,Modified,ModifiedBy")] Permission permission)
        {
            if (ModelState.IsValid)
            {
                permissonDao.Update(permission);
                return RedirectToAction("Index");
            }
            ViewBag.ModuleId = new SelectList(moduleDao.Select(), "Id", "Name", permission.ModuleId);
            return View(permission);
        }

        // GET: /Permission/Delete/5
        public ActionResult Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Permission permission = permissonDao.Select(id.Value);
            if (permission == null)
            {
                return HttpNotFound();
            }
            return View(permission);
        }

        // POST: /Permission/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            permissonDao.Delete(id);
            return RedirectToAction("Index");
        }
    }
}
